﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.WaterPurchase;

namespace University.Workshop.Presentation.WaterPurchase
{
    /// <summary>
    /// Interaction logic for PageSecondStepModifyWaterPurchase.xaml
    /// </summary>
    public partial class PageSecondStepModifyWaterPurchase : Page, ISecondStepForModifyWaterPurchase
    {
        PresenterSecondStepForModifyWaterPurchase pSecondStepForModifyWaterPurchase;
        PresenterModifyWaterPurchaseView pInternal;

        public PageSecondStepModifyWaterPurchase(PresenterModifyWaterPurchaseView pExternal)
        {
            InitializeComponent();
            pSecondStepForModifyWaterPurchase = new PresenterSecondStepForModifyWaterPurchase(this);
            pInternal = pExternal;

            //FechaActualLbl.Content = DateTime.Now.ToShortDateString();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Modificar Compra";
        }

        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }

        private void MountsTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch));
            }
        }

        private void MountsTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }



        #region IRegisterBasicView Members

        public Model.WaterPurchase WaterPurchaseToRegister
        {
            get;
            set;
        }

        public void Update()
        {
            pSecondStepForModifyWaterPurchase.UpdateData();
        }

        public string SystemMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                pInternal.NotifySystemMessage(value);
            }
        }

        public string ErrorMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                pInternal.NotifyErrorMessage(value);
            }
        }

        public void ChargeDataToBasic()
        {
            pSecondStepForModifyWaterPurchase.ChargeDataUser();
        }

        public void GoNextPage()
        {
        }

        public void GoBackPage()
        {
            pInternal.GoStepOne();
        }

        #endregion


        #region ISecondStepForModifyWaterPurchase Members

        public int Amount
        {
            get
            {
                if (AmountTxt.Text != "")
                    return int.Parse(AmountTxt.Text);
                else
                    return 0;
            }
            set
            {
                AmountTxt.Text = value.ToString();
            }
        }

        public DateTime DateRegistry
        {
            set
            {
                FechaActualLbl.Content = value.ToShortDateString();
            }
        }

        public DateTime DatePurchase
        {
            get
            {
                return DateTime.Parse(DatePurchaseDpk.Text);
            }
            set
            {
                //if (value.ToString() == "01/01/0001 12:00:00 a.m.")
                //    DatePurchaseDpk.Text = DateTime.Today.ToString();
                //else
                    DatePurchaseDpk.Text = value.ToString();
            }
        }

        public bool Courtesy
        {
            get
            {
                return CourtesyChk.IsChecked.Value;
            }
            set
            {
                CourtesyChk.IsChecked = value;
            }
        }

        public decimal TotalPrice
        {
            get
            {
                if (TotalPriceTxt.Text != "")
                    return decimal.Parse(TotalPriceTxt.Text);
                else
                    return 0;
            }
            set
            {
                decimal monto = value;
                //Math.Truncate(monto);
                int entero = Convert.ToInt32(monto);
                TotalPriceTxt.Text = entero.ToString();
            }
        }

        #endregion

        #region IModifyBasicView Members

        public Model.WaterPurchase WaterPurchaseToModify
        {
            get;
            set;
        }

        #endregion

        private void CourtesyChk_Checked(object sender, RoutedEventArgs e)
        {
            TotalPriceTxt.IsEnabled = false;
            TotalPriceTxt.Text = (0).ToString();
        }

        private void CourtesyChk_Unchecked(object sender, RoutedEventArgs e)
        {
            TotalPriceTxt.IsEnabled = true;
        }

        private void DatePurchaseDpk_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = true;
        }
    }
}
